<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/myblog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/myblog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/myblog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/myblog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="NodeJS,debug,调试工具," />





  <link rel="alternate" href="/myblog/atom.xml" title="骑马揍比尔" type="application/atom+xml" />






<meta name="description" content="Node 调试工具入门教程            Node 调试工具入门教程                                        JavaScript 程序越来越复杂，调试工具的重要性日益凸显。客户端脚本有浏览器，Node 脚本怎么调试呢？">
<meta name="keywords" content="NodeJS,debug,调试工具">
<meta property="og:type" content="article">
<meta property="og:title" content="Node 调试工具入门教程">
<meta property="og:url" content="http://flzc.gitee.io/myblog/2018/04/04/20180404_Node 调试工具入门教程/index.html">
<meta property="og:site_name" content="骑马揍比尔">
<meta property="og:description" content="Node 调试工具入门教程            Node 调试工具入门教程                                        JavaScript 程序越来越复杂，调试工具的重要性日益凸显。客户端脚本有浏览器，Node 脚本怎么调试呢？">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031901.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031902.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031903.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031904.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031905.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031906.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031907.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031908.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031909.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031910.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031911.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031912.png">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031913.png">
<meta property="og:updated_time" content="2018-04-04T07:55:42.021Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Node 调试工具入门教程">
<meta name="twitter:description" content="Node 调试工具入门教程            Node 调试工具入门教程                                        JavaScript 程序越来越复杂，调试工具的重要性日益凸显。客户端脚本有浏览器，Node 脚本怎么调试呢？">
<meta name="twitter:image" content="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031901.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/myblog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://flzc.gitee.io/myblog/2018/04/04/20180404_Node 调试工具入门教程/"/>





  <title>Node 调试工具入门教程 | 骑马揍比尔</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/myblog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">骑马揍比尔</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">——大猛子</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/myblog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/myblog/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/myblog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/myblog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/myblog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://flzc.gitee.io/myblog/myblog/2018/04/04/20180404_Node 调试工具入门教程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="骑马揍比尔">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="骑马揍比尔">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Node 调试工具入门教程</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-04T15:55:42+08:00">
                2018-04-04
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/NodeJS/" itemprop="url" rel="index">
                    <span itemprop="name">NodeJS</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/NodeJS/调试工具/" itemprop="url" rel="index">
                    <span itemprop="name">调试工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/myblog/2018/04/04/20180404_Node 调试工具入门教程/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/myblog/2018/04/04/20180404_Node 调试工具入门教程/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,308
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard"><br><br><head><br>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>    <meta name="generator" content="Movable Type  5.2.2"><br>    <meta name="viewport" content="width=device-width, initial-scale=1"><br>    <link rel="stylesheet" href="http://www.ruanyifeng.com/blog/styles.css" type="text/css"><br>    <link rel="start" href="http://www.ruanyifeng.com/blog/" title="Home"><br>    <link rel="alternate" type="application/atom+xml" title="Recent Entries" href="http://feeds.feedburner.com/ruanyifeng"><br>    <script type="text/javascript" src="http://www.ruanyifeng.com/blog/mt.js"></script><br>    <title>Node 调试工具入门教程</title><br></head><br><br><body id="scrapbook" class="mt-entry-archive one-column"><br><br><br><br><br><br>    <article class="hentry"><br>        <h1 id="page-title" class="asset-name entry-title">Node 调试工具入门教程</h1><br>        <div id="share_button" class="social-share" style="float:right;padding-right:2em;padding-top:1em;"></div><br><br><br>        <div class="asset-content entry-content" id="main-content"><br><br>            <!-- div class="asset-body" --><br>            <p>JavaScript 程序越来越复杂，调试工具的重要性日益凸显。客户端脚本有浏览器，Node 脚本怎么调试呢？</p><br><br>            <!-- /div --><br><br><br>            <!-- div id="more" class="asset-more" --><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031901.png" alt="" title=""><br>            </p><br><br>            <p>2016年，Node 决定将 Chrome 浏览器的”开发者工具”作为官方的调试工具，使得 Node 脚本也可以使用图形界面调试，这大大方便了开发者。</p><br><br>            <p>本文介绍如何使用 Node 脚本的调试工具。</p><br><br>            <h2>一、示例程序</h2><br><br>            <p>为了方便讲解，下面是一个示例脚本。首先，新建一个工作目录，并进入该目录。</p><br><br><br>            <blockquote><br>                <pre><br>                    <code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir debug-demo</span><br></pre></td></tr></table></figure><br><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cd debug-demo</span><br></pre></td></tr></table></figure><br><br>                </code><br>            </pre><br>            </blockquote><br><br>            <p>然后，生成<br>                <code>package.json</code>文件，并安装<br>                <a href="http://www.ruanyifeng.com/blog/2017/08/koa.html" target="_blank" rel="noopener">Koa</a> 框架和 koa-route 模块。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ npm init -y</span><br><span class="line">$ npm install --save koa koa-route</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>接着，新建一个脚本<br>                <code>app.js</code>，并写入下面的内容。</p><br><br>            <blockquote><br>                <pre><code class="language-javascript"><br>// app.js<br>const Koa = require(‘koa’);<br>const router = require(‘koa-route’);<br><br>const app = new Koa();<br><br>const main = ctx =&gt; {<br>  ctx.response.body = ‘Hello World’;<br>};<br><br>const welcome = (ctx, name) =&gt; {<br>  ctx.response.body = ‘Hello ‘ + name;<br>};<br><br>app.use(router.get(‘/‘, main));<br>app.use(router.get(‘/:name’, welcome));<br><br>app.listen(3000);<br>console.log(‘listening on port 3000’);<br></code></pre><br>            </blockquote><br><br>            <p>上面代码是一个简单的 Web 应用，指定了两个路由，访问后会显示一行欢迎信息。如果想了解代码的详细含义，可以参考<br>                <a href="http://www.ruanyifeng.com/blog/2017/08/koa.html" target="_blank" rel="noopener">Koa 教程</a>。</p><br><br>            <h2>二、启动开发者工具</h2><br><br>            <p>现在，运行上面的脚本。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ node --inspect app.js</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>上面代码中，<br>                <code>–inspect</code>参数是启动调试模式必需的。这时，打开浏览器访问<br>                <code><a href="http://127.0.0.1:3000" target="_blank" rel="noopener">http://127.0.0.1:3000</a></code>，就可以看到 Hello World 了。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031902.png" alt="" title=""><br>            </p><br><br>            <p>接下来，就要开始调试了。一共有两种打开调试工具的方法，第一种是在 Chrome 浏览器的地址栏，键入<br>                <code>chrome://inspect</code>或者<br>                <code>about:inspect</code>，回车后就可以看到下面的界面。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031903.png" alt="" title=""><br>            </p><br><br>            <p>在 Target 部分，点击 inspect 链接，就能进入调试工具了。</p><br><br>            <p>第二种进入调试工具的方法，是在 <a href="http://127.0.0.1:3000" target="_blank" rel="noopener">http://127.0.0.1:3000</a> 的窗口打开”开发者工具”，顶部左上角有一个 Node 的绿色标志，点击就可以进入。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031904.png" alt="" title=""><br>            </p><br><br>            <h2>三、调试工具窗口</h2><br><br>            <p>调试工具其实就是”开发者工具”的定制版，省去了那些对服务器脚本没用的部分。</p><br><br>            <p>它主要有四个面板。</p><br><br>            <blockquote><br>                <ul><br>                    <li>Console：控制台</li><br>                    <li>Memory：内存</li><br>                    <li>Profiler：性能</li><br>                    <li>Sources：源码</li><br>                </ul><br>            </blockquote><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031905.png" alt="" title=""><br>            </p><br><br>            <p>这些面板的用法，基本上跟浏览器环境差不多，这里只介绍 Sources （源码）面板。</p><br><br>            <h2>四、设置断点</h2><br><br>            <p>进入 Sources 面板，找到正在运行的脚本<br>                <code>app.js</code>。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031906.png" alt="" title=""><br>            </p><br><br>            <p>在第11行（也就是下面这一行）的行号上点一下，就设置了一个断点。</p><br><br>            <blockquote><br>                <pre><code class="language-javascript"><br>ctx.response.body = ‘Hello ‘ + name;<br></code></pre><br>            </blockquote><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031907.png" alt="" title=""><br>            </p><br><br>            <p>这时，浏览器访问 <a href="http://127.0.0.1:3000/alice" target="_blank" rel="noopener">http://127.0.0.1:3000/alice</a> ，页面会显示正在等待服务器返回。切换到调试工具，可以看到 Node 主线程处于暂停（paused）阶段。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031908.png" alt="" title=""><br>            </p><br><br>            <p>进入 Console 面板，输入 name，会返回 alice。这表明我们正处在断点处的上下文（context）。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031909.png" alt="" title=""><br>            </p><br><br>            <p>再切回 Sources 面板，右侧可以看到 Watch、Call Stack、Scope、Breakpoints 等折叠项。打开 Scope 折叠项，可以看到 Local 作用域和 Global 作用域里面的所有变量。</p><br><br>            <p>Local 作用域里面，变量<br>                <code>name</code>的值是<br>                <code>alice</code>，双击进入编辑状态，把它改成<br>                <code>bob</code>。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031910.png" alt="" title=""><br>            </p><br><br>            <p>然后，点击顶部工具栏的继续运行按钮。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031911.png" alt="" title=""><br>            </p><br><br>            <p>页面上就可以看到 Hello bob 了。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031912.png" alt="" title=""><br>            </p><br><br>            <p>命令行下，按下 ctrl + c，终止运行<br>                <code>app.js</code>。</p><br><br>            <h2>五、调试非服务脚本</h2><br><br>            <p>Web 服务脚本会一直在后台运行，但是大部分脚本只是处理某个任务，运行完就会终止。这时，你可能根本没有时间打开调试工具。等你打开了，脚本早就结束运行了。这时怎么调试呢？</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ node --inspect=9229 -e &quot;setTimeout(function() &#123; console.log(&apos;yes&apos;); &#125;, 30000)&quot;</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>上面代码中，<br>                <code>–inspect=9229</code>指定调试端口为 9229，这是调试工具默认的通信端口。<br>                <code>-e</code>参数指定一个字符串，作为代码运行。</p><br><br>            <p>访问<br>                <code>chrome://inspect</code>，就可以进入调试工具，调试这段代码了。</p><br><br>            <p><br>                <img src="http://www.ruanyifeng.com/blogimg/asset/2018/bg2018031913.png" alt="" title=""><br>            </p><br><br>            <p>代码放在<br>                <code>setTimeout</code>里面，总是不太方便。那些运行时间较短的脚本，可能根本来不及打开调试工具。这时就要使用下面的方法。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ node --inspect-brk=9229 app.js</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>上面代码中，<br>                <code>–inspect-brk</code>指定在第一行就设置断点。也就是说，一开始运行，就是暂停的状态。</p><br><br>            <h2>六、忘了写 –inspect 怎么办？</h2><br><br>            <p>打开调试工具的前提是，启动 Node 脚本时就加上<br>                <code>–inspect</code>参数。如果忘了这个参数，还能不能调试呢？</p><br><br>            <p>回答是可以的。首先，正常启动脚本。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ node app.js</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>然后，在另一个命令行窗口，查找上面脚本的进程号。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ ps ax | grep app.js </span><br><span class="line"></span><br><span class="line">30464 pts/11   Sl+    0:00 node app.js</span><br><span class="line">30541 pts/12   S+     0:00 grep app.js</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>上面命令中，<br>                <code>app.js</code>的进程号是<br>                <code>30464</code>。</p><br><br>            <p>接着，运行下面的命令。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ node -e &apos;process._debugProcess(30464)&apos;</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <p>上面命令会建立进程 30464 与调试工具的连接，然后就可以打开调试工具了。</p><br><br>            <p>还有一种方法，就是向脚本进程发送<br>                <a href="https://stackoverflow.com/questions/13052548/node-js-how-to-attach-to-a-running-process-and-to-debug-the-server-with-a-conso" target="_blank" rel="noopener">SIGUSR1</a> 信号，也可以建立调试连接。</p><br><br>            <blockquote><br>                <pre><code class="language-bash"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ kill -SIGUSR1 30464</span><br></pre></td></tr></table></figure><br><br></code></pre><br>            </blockquote><br><br>            <h2>七、参考链接</h2><br><br>            <ul><br>                <li><br>                    <a href="https://medium.com/the-node-js-collection/debugging-node-js-with-google-chrome-4965b5f910f4" target="_blank" rel="noopener">Debugging Node.js with Google Chrome</a>, by Jacopo Daeli</li><br>                <li><br>                    <a href="https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27" target="_blank" rel="noopener">Debugging Node.js with Chrome DevTools</a>, by Paul Irish</li><br>                <li><br>                    <a href="https://remysharp.com/2018/03/03/last-minute-node-debugging" target="_blank" rel="noopener">Last minute node debugging</a>, by Remy Sharp</li><br>            </ul><br><br>            <p>（完）</p><br><br>            <!-- /div --><br><br>        </div><br>    </article><br><br></body><br><br></html>
      
    </div>
    
    
    


<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">
		-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------
		</div>
    
</div>
  
</div>


    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/myblog/tags/NodeJS/" rel="tag"># NodeJS</a>
          
            <a href="/myblog/tags/debug/" rel="tag"># debug</a>
          
            <a href="/myblog/tags/调试工具/" rel="tag"># 调试工具</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/myblog/2018/04/03/20180326_node.js配置访问本地的html文件/" rel="next" title="node.js配置访问本地的html文件">
                <i class="fa fa-chevron-left"></i> node.js配置访问本地的html文件
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
   <!--MOB SHARE BEGIN-->
<div class="-mob-share-ui-button -mob-share-open">分享</div>
<div class="-mob-share-ui" style="display: none">
    <ul class="-mob-share-list">
        <li class="-mob-share-weibo"><p>新浪微博</p></li>
        <li class="-mob-share-tencentweibo"><p>腾讯微博</p></li>
        <li class="-mob-share-qzone"><p>QQ空间</p></li>
        <li class="-mob-share-qq"><p>QQ好友</p></li>
        <li class="-mob-share-renren"><p>人人网</p></li>
        <li class="-mob-share-kaixin"><p>开心网</p></li>
        <li class="-mob-share-douban"><p>豆瓣</p></li>
        <li class="-mob-share-facebook"><p>Facebook</p></li>
        <li class="-mob-share-twitter"><p>Twitter</p></li>
    </ul>
    <div class="-mob-share-close">取消</div>
</div>
<div class="-mob-share-ui-bg"></div>
<script id="-mob-share" src="http://f1.webshare.mob.com/code/mob-share.js?appkey=appkey"></script>
<!--MOB SHARE END-->

      


    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/myblog/uploads/avatar.jpg"
                alt="骑马揍比尔" />
            
              <p class="site-author-name" itemprop="name">骑马揍比尔</p>
              <p class="site-description motion-element" itemprop="description">当你安逸时，生活一定会给你空血的暴击，只剩最后一滴血，享受恢复的痛苦。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/myblog/archives/">
              
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/myblog/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/myblog/tags/index.html">
                  <span class="site-state-item-count">17</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/myblog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/PresidentFu" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="biubiu200@163.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://weibo.com/u/5648936534" target="_blank" title="Sina">
                      
                        <i class="fa fa-fw fa-weibo"></i>Sina</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yourname" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#page-title"><span class="nav-number">1.</span> <span class="nav-text">Node 调试工具入门教程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.1.</span> <span class="nav-text">一、示例程序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.2.</span> <span class="nav-text">二、启动开发者工具</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.3.</span> <span class="nav-text">三、调试工具窗口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.4.</span> <span class="nav-text">四、设置断点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.5.</span> <span class="nav-text">五、调试非服务脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.6.</span> <span class="nav-text">六、忘了写 –inspect 怎么办？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.7.</span> <span class="nav-text">七、参考链接</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">骑马揍比尔</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">4.6k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/myblog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/myblog/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/myblog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/myblog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/myblog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/myblog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/myblog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/myblog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/myblog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 's8fDvwFbBVPzL0wRR7LchiC5-gzGzoHsz',
        appKey: '0u6LwCiQFl2jTg1buqwe7OLb',
        placeholder: '不服来辩，嘿嘿',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/myblog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  


</body>
</html>